package com.maxd.service.common;

import com.google.common.collect.Maps;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.CommonInfo;
import com.maxd.respository.common.CommonJpaRepository;
import com.maxd.respository.common.CommonRepository;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Service
@Slf4j
public class CommonServiceImpl implements ICommonService {

    @Autowired
    private CommonJpaRepository appJpaRepository;

    @Autowired
    private CommonRepository commonRepository;

    private Map<Integer, CommonInfo> configMap;

    @Override
    public Result findAll() {
        return ResultUtil.success(appJpaRepository.findAll());
    }

    @Override
    public Result saveBody(CommonInfo commonInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        commonInfo.setCreateAt(sdf.format(now));
        CommonInfo one = commonRepository.findOne(commonInfo.getType());
        CommonInfo respEntity = null;
        if (one != null) {
            one.setValue(commonInfo.getValue());
            respEntity = appJpaRepository.save(one);
        } else {
            respEntity = appJpaRepository.save(commonInfo);
        }
        // 异步重新加载缓存配置
        CompletableFuture.runAsync(() -> loadConfigs(true));
        return ResultUtil.success(respEntity);
    }


    @Override
    public Result findOne(long id) {
        return ResultUtil.success(appJpaRepository.findById(id).orElse(null));
    }

    @Override
    public Result delete(long id) {
        appJpaRepository.deleteById(id);
        // 异步重新加载缓存配置
        CompletableFuture.runAsync(() -> loadConfigs(true));
        return ResultUtil.success();
    }

    @Override
    @Transactional
    public Result updateBody(CommonInfo commonInfo) {
        CommonInfo commonInfo1 = appJpaRepository.findById(commonInfo.getId()).orElse(null);
        assert commonInfo1 != null;
        BeanUtils.copyProperties(commonInfo, commonInfo1);
        return ResultUtil.success();
    }

    @Override
    public Result findByType(Integer type) {
        return ResultUtil.success(commonRepository.findOne(type));
    }

    @Override
    public Result findByTypeAndCondition(String condition) {
        return ResultUtil.success(commonRepository.findByCondition(condition));
    }

    @Override
    public CommonInfo findByTypeCache(Integer type) {
        if(configMap == null) {
            loadConfigs(false);
        }
        return configMap.get(type);
    }

    @Override
    public synchronized void loadConfigs(boolean reload) {
        log.info("加载配置，是否重新加载:{}", reload);
        if(configMap == null || reload) {
            Map<Integer, CommonInfo> tempMap = Maps.newHashMap();
            List<CommonInfo> list = appJpaRepository.findAll();
            for (CommonInfo commonInfo : list) {
                tempMap.put(commonInfo.getType(), commonInfo);
            }
            configMap = tempMap;
        }
    }
}
